<template>
  <div class="app-container">
    <el-form ref="order" :model="order" :rules="rules" label-width="120px">

      <el-form-item prop="userId" label="用户姓名">
        <el-select
          v-model="order.userId"
          :style="{width: '25%'}"
          placeholder="请选择用户id"
          filterable
          clearable>
          <el-option v-for="(user,key) in userList" :key="key" :value="user.id" :label="user.name"/>
        </el-select>
      </el-form-item>

      <el-form-item label="靶场id" prop="trainingId">
        <el-input v-model="order.trainingId" controls-position="right" />
      </el-form-item>

      <el-form-item label="支付宝交易编号" prop="mobile">
        <el-input v-model="order.tradeNo" :style="{width: '25%'}" placeholder="请输入交易编号" clearable/>
      </el-form-item>

      <el-form-item label="价格" prop="price">
        <el-input v-model="order.price" />
      </el-form-item>

      <el-form-item label="支付状态" prop="status">
        <el-select v-model="order.status" clearable placeholder="请选择">
          <el-option :value="0" label="待支付"/>
          <el-option :value="1" label="已支付"/>
        </el-select>
      </el-form-item>

      <el-form-item>
        <el-button :disabled="saveBtnDisabled" type="primary" @click="addOrUpdate">保存</el-button>
        <el-button @click="resetForm">重置</el-button>
      </el-form-item>

    </el-form>
  </div>
</template>

<script>

import order from '../../api/order'
import user from '../../api/user'

export default {
  data() {
    return {
      order: {
        userId: '',
        trainingId: '',
        tradeNo: '',
        price: undefined,
        status: 0

      },
      userList: [],
      saveBtnDisabled: false,
      rules: {
        userId: [
          { required: true, message: '请输入用户id' },
          { trigger: 'change' }
        ],
        trainingId: [
          {
            required: true,
            message: '请输入靶场id' },
          { trigger: 'change' }
        ],
        tradeNo: [
          {
            required: true,
            message: '支付宝交易编号' },
          { trigger: 'change' }
        ],
        price: [
          {
            required: true,
            message: '靶场价格' },
          { trigger: 'change' }
        ],
        status: [
          { required: true, message: '请选择支付状态', trigger: 'change' }
        ]
      }
    }
  },
  created() {
    this.listUser()
    if (this.$route.params && this.$route.params.id) {
      const id = this.$route.params.id
      this.findById(id)
    }
  },
  methods: {
    listUser() {
      user.listAll().then(result => {
        this.userList = result.data
        console.log(this.userList)
      })
    },
    resetForm() {
      this.$refs['order'].resetFields()
    },

    addOrUpdate() {
      this.$refs['order'].validate((valid) => {
        if (valid) {
          if (this.order.id != null) {
            this.updateOrder()
          } else {
            console.log(this.order.id)
            this.addOrder()
          }
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    addOrder() {
      order.addOrder(this.order).then(res => {
        this.$message.success(res.message)
        this.$router.push({ path: '/order/list' })
      })
    },
    updateOrder() {
      order.updateOrder(this.order).then(res => {
        this.$message.success(res.message)
        this.$router.push({ path: '/order/list' })
      })
    },

    findById(id) {
      order.findById(id).then(res => {
        this.order = res.data
      })
    }
  }
}
</script>
<style>
.avatar-uploader .el-upload {
  border: 1px dashed #d9d9d9;
  border-radius: 6px;
  cursor: pointer;
  position: relative;
  overflow: hidden;
}

.avatar-uploader .el-upload:hover {
  border-color: #409EFF;
}

.avatar-uploader-icon {
  font-size: 28px;
  color: #8c939d;
  width: 178px;
  height: 178px;
  line-height: 178px;
  text-align: center;
}

.avatar {
  width: 178px;
  height: 178px;
  display: block;
}
</style>
